package com.hy.formula.function;

import static org.hamcrest.CoreMatchers.instanceOf;

import java.math.BigDecimal;

import com.hy.formula.common.BigDecimalUtils;
import com.hy.formula.common.CollectionUtils;
import com.hy.formula.exception.FormulaException;
import com.hy.formula.runtime.FRuntimeContext;

public class MoneyFunc implements IFunction {

	@Override
	public String getName() {
		return "money";
	}

	@Override
	public Object execute(FRuntimeContext context, Object... params) {
		if(CollectionUtils.isEmpty(params)) {
			throw new FormulaException("参数数量不对");
		}
		if(params[0] instanceof BigDecimal) {
			return BigDecimalUtils.toMoneyStr((BigDecimal)params[0]);
		}else if(params[0] instanceof Double) {
			return String.format("%.2f", params[0]);
		}else if(params[0] instanceof Float) {
			return String.format("%.2f", params[0]);
		}else if(params[0] instanceof Integer) {
			return String.format("%.2f", Float.valueOf(params[0].toString()));
		}else {
			return params[0];
		}
	}

	@Override
	public String description() {
		
		return "结果值格式化成2位小数，以字符串输出";
	}

}
